package com.lkl.demo.provider.controller;

import com.lkl.demo.common.constant.RabbitMQConstant;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userNew")
public class UserNewController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 用户注册
     */
    @PostMapping("/regist")
    public Map<String, String> register(String username, String password) throws InterruptedException {
        long start = System.currentTimeMillis();
        // 1、数据库新增数据
        Thread.sleep(1000);
        System.out.println("数据库新增成功，" + username);

        // 2、向消息队列发送消息
        Map<String, String> message = new HashMap<>();
        message.put(RabbitMQConstant.MESSAGE_TYPE, "regist");
        message.put("phone", "18712501935");
        message.put("email", "2479076447@qq.com");

        // 三个参数：交换机名称，发送到交换机路由值，消息
        amqpTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_NAME, RabbitMQConstant.ROUTER_KEY_EXCHANGE, message);

        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "注册成功");
        map.put("code", "200");
        map.put("costTime", System.currentTimeMillis() - start + "ms");
        return map;
    }

    /**
     * 用户注销
     */
    @PutMapping("/logout")
    public Map<String, String> logout(String username, String password) throws InterruptedException {
        long start = System.currentTimeMillis();
        // 1、数据库修改数据
        Thread.sleep(1000);
        System.out.println("数据库修改成功，" + username);

        // 2、向消息队列发送消息
        Map<String, String> message = new HashMap<>();
        message.put(RabbitMQConstant.MESSAGE_TYPE, "logout");
        message.put("phone", "18712501935");
        message.put("email", "2479076447@qq.com");

        // 三个参数：交换机名称，发送到交换机路由值，消息
        amqpTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_NAME, RabbitMQConstant.ROUTER_KEY_EXCHANGE, message);

        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "注销成功");
        map.put("code", "200");
        map.put("costTime", System.currentTimeMillis() - start + "ms");
        return map;
    }

}
